#region File Description
//-----------------------------------------------------------------------------
// Game.cs
//
// Microsoft XNA Community Game Platform
// Copyright (C) Microsoft Corporation. All rights reserved.
//-----------------------------------------------------------------------------
#endregion

#region Using Statements
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;
#endregion

namespace GameStateManagement
{
    /// <summary>
    /// Sample showing how to manage different game states, with transitions
    /// between menu screens, a loading screen, the game itself, and a pause
    /// menu. This main game class is extremely simple: all the interesting
    /// stuff happens in the ScreenManager component.
    /// </summary>
    public class GameStateManagementGame : Microsoft.Xna.Framework.Game
    {


        GraphicsDeviceManager graphics;
        ScreenManager screenManager;
        public Utils.FrameRateCounter m_kFrameRate;
        public RenderTarget2D renderTarget;
        protected Effect outlineEffect;
        SpriteBatch spriteBatch;


		public float fTargetMsPerFrame
		{
			get { return (float)TargetElapsedTime.Milliseconds; }
			set 
			{ 
				if (value > 1.0f)
					TargetElapsedTime = System.TimeSpan.FromMilliseconds(value); 
				else
					TargetElapsedTime = System.TimeSpan.FromMilliseconds(1.0f); 
			}
		}



        public GameStateManagementGame()
        {
            Content.RootDirectory = "Content";

            graphics = new GraphicsDeviceManager(this);

            graphics.PreferredBackBufferWidth = 1024;
            graphics.PreferredBackBufferHeight = 768;

            // Create the screen manager component.
            screenManager = new ScreenManager(this);

            Components.Add(screenManager);

            // Activate the first screens.
            screenManager.AddScreen(new BackgroundScreen());
            screenManager.AddScreen(new MainMenuScreen());

            m_kFrameRate = new Utils.FrameRateCounter(this, new Vector2(750.0f, 600.0f));
            Components.Add(m_kFrameRate);

            // For testing purposes, let's disable fixed time step and vsync.
            IsFixedTimeStep = true;
            graphics.SynchronizeWithVerticalRetrace = false;
        }

        protected override void LoadContent()
        {
            base.LoadContent();

            ContentManager contentManager = new ContentManager(Services, "Content");

            PresentationParameters pp = GraphicsDevice.PresentationParameters;
            renderTarget = new RenderTarget2D(GraphicsDevice, pp.BackBufferWidth, pp.BackBufferHeight, 2, GraphicsDevice.DisplayMode.Format);
            outlineEffect = contentManager.Load<Effect>("OutLineShader");

            spriteBatch = new SpriteBatch(GraphicsDevice);
        }



        protected override void Draw(GameTime gameTime)
        {
            graphics.GraphicsDevice.Clear(Color.Black);

            // The real drawing happens inside the screen manager component.
            GraphicsDevice.SetRenderTarget(0, renderTarget);
            base.Draw(gameTime);
            GraphicsDevice.SetRenderTarget(0, null);

            Texture2D SceneTexture = renderTarget.GetTexture();

            /*  outlineEffect.CurrentTechnique = outlineEffect.Techniques["PostOutline"];
              changeEffectUsedByModel(actorModel, outlineEffect);

              foreach (ModelMesh mesh in actorModel.Meshes)
              {
                  mesh.Draw();
              }*/

            GraphicsDevice.Clear(ClearOptions.Target, Color.DarkSlateBlue, 1.0f, 0);


            spriteBatch.Begin(SpriteBlendMode.None, SpriteSortMode.Immediate, SaveStateMode.SaveState);
            {
                // Apply the post process shader
                outlineEffect.Begin();
                {
                    outlineEffect.CurrentTechnique.Passes[0].Begin();
                    {
                        outlineEffect.Parameters["Thickness"].SetValue(2.0f);
                        outlineEffect.Parameters["Threshold"].SetValue(0.25f);

                        spriteBatch.Draw(SceneTexture, new Rectangle(0, 0, 1024, 768), Color.White);
                        outlineEffect.CurrentTechnique.Passes[0].End();
                    }
                }
                outlineEffect.End();
            }
            spriteBatch.End();

        }


    }

    /// <summary>
    /// The main entry point for the application.
    /// </summary>
    static class Program
    {
        static void Main()
        {
            using (GameStateManagementGame game = new GameStateManagementGame())
            {
                game.Run();
            }
        }
    }

}
